﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Entity.ViewModel;
using DLMemberPointMS.Entity.DTOModel;

using DLMemberPointMS.Business;
using DLMemberPointMS.Common;
using System.Web.Security;
using System.Web;

namespace DLMemberPointMS.Business
{
    public class UsersService : BaseService<Users>
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public OperateResult Signin(UsersViewModel ViewModel)
        {
            var model = Find(u => u.U_LoginName == ViewModel.U_LoginName);
            if (model != null)
            {
                if (model.U_Password == ViewModel.U_Password)
                {
                    var DtoModel = new UsersDtoMdel
                    {
                        U_LoginName = model.U_LoginName,
                        S_ID = model.S_ID,
                        U_ID = model.U_ID,
                        U_RealName = model.U_RealName,
                        U_CanDelete = model.U_CanDelete,
                        U_Role = model.U_Role,
                        U_Sex = model.U_Sex,
                        U_Telephone = model.U_Telephone
                    };
                    SetUsersData(DtoModel);
                    return new OperateResult(code.Success, "登陆成功", DtoModel);
                }
                else
                {
                    return new OperateResult(code.Error, "密码错误");
                }
            }
            else
            {
                return new OperateResult(code.Error, "用户不存在");
            }
        }
        /// <summary>
        /// 写入票据
        /// </summary>
        /// <param name="usersDtoModel"></param>
        public void SetUsersData(UsersDtoMdel usersDtoModel)
        {
            //获取用户信息，转换成Json
            var UserData = usersDtoModel.ToJson();
            //创建票据对象
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, "U_LoginName", DateTime.Now, DateTime.Now.AddDays(1), false, UserData);
            //给票据加密
            var ticketEncrpt = FormsAuthentication.Encrypt(ticket);
            //创建Cookies
            HttpCookie cookies= new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrpt);
            cookies.Expires = DateTime.Now.Add(FormsAuthentication.Timeout);
            cookies.Domain = FormsAuthentication.CookieDomain;
            cookies.HttpOnly = true;
            cookies.Secure = FormsAuthentication.RequireSSL;
            cookies.Path = FormsAuthentication.FormsCookiePath;
            //获取HttpContext上下文对象
            HttpContext context = HttpContext.Current;
            if (context == null)
            {
                throw new ArgumentNullException("Context为空");
            }
            //写入Cookies
            context.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
            context.Response.Cookies.Add(cookies);

        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public OperateResult Logout()
        {
            HttpContext context = HttpContext.Current;
            context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);
            return new OperateResult(code.Success, "已退出");
        }
        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult ShowUserInfo(int id)
        {
            var data = Find(u => u.U_ID == id);
            var viewmod = new UsersViewModel
            {
                U_LoginName = data.U_LoginName,
                U_RealName = data.U_RealName,
                U_Sex = data.U_Sex == "男" ? "1" : "0",
                U_Telephone = data.U_Telephone,
                U_ID = data.U_ID
            };
            return new OperateResult(code.Success, "ok", viewmod);
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult ChangeInfo(UsersViewModel mod)
        {
            var data = Find(u => u.U_ID == mod.U_ID);
            if (mod.U_LoginName != data.U_LoginName)
                if (Find(u => u.U_LoginName == mod.U_LoginName) != null)
                    return new OperateResult(code.Error, "用户名已存在，请更换");
            data.U_LoginName = mod.U_LoginName;
            data.U_RealName = mod.U_RealName;
            data.U_Sex = mod.U_Sex == "1" ? "男" : "女";
            data.U_Telephone = mod.U_Telephone;
            if (Update(data))
            {
                return new OperateResult(code.Success, "修改成功");
            }
            else
            {
                return new OperateResult(code.Error, "修改失败，请重试");
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult ChangePwd(UsersViewModel mod)
        {
            var data = Find(u => u.U_ID == mod.U_ID);
            if (data != null)
            {
                if (data.U_Password != mod.U_Password)
                {
                    return new OperateResult(code.Error, "原始密码错误，请重试");
                }
                if (!mod.newU_Password.Equals(mod.confifmU_Password))
                {
                    return new OperateResult(code.Error, "两次驶入密码不一致");
                }
                data.U_Password = mod.newU_Password;
                if (Update(data))
                {
                    return new OperateResult(code.Success, "修改成功");
                }
                return new OperateResult(code.Error, "修改失败，请重试");
            }
            else
            {
                return new OperateResult(code.Error, "用户不存在");
            }
        }
    }
}
